{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "04184.ipynb",
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/yananma/5_programs_per_day/blob/master/04184.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "NH4nYemxaQlo",
        "colab_type": "text"
      },
      "source": [
        "## 5.6 深度卷积神经网络 ( AlexNet )"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "oMSU57iQXJ7M",
        "colab_type": "text"
      },
      "source": [
        "### 5.6.2 AlexNet"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WTxSzA1iasXZ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torch \n",
        "from torch import nn, optim \n",
        "import torchvision \n",
        "import d2l \n",
        "import time \n",
        "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
        "\n",
        "\n",
        "class AlexNet(nn.Module):\n",
        "    def __init__(self):\n",
        "        super(AlexNet, self).__init__()\n",
        "        self.conv = nn.Sequential(\n",
        "            nn.Conv2d(1, 96, 11, 4), \n",
        "            nn.ReLU(), \n",
        "            nn.MaxPool2d(3, 2), \n",
        "            nn.Conv2d(96, 256, 5, 1, 2), \n",
        "            nn.ReLU(), \n",
        "            nn.MaxPool2d(3, 2), \n",
        "            nn.Conv2d(256, 384, 3, 1, 1), \n",
        "            nn.ReLU(),\n",
        "            nn.Conv2d(384, 384, 3, 1, 1), \n",
        "            nn.ReLU(), \n",
        "            nn.Conv2d(384, 256, 3, 1, 1), \n",
        "            nn.ReLU(), \n",
        "            nn.MaxPool2d(3, 2),\n",
        "        )\n",
        "        self.fc = nn.Sequential(\n",
        "            nn.Linear(256*5*5, 4096), \n",
        "            nn.ReLU(), \n",
        "            nn.Dropout(0.5), \n",
        "            nn.Linear(4096, 4096), \n",
        "            nn.ReLU(), \n",
        "            nn.Dropout(0.5), \n",
        "            nn.Linear(4096, 10), \n",
        "        )\n",
        "\n",
        "    def forward(self, img):\n",
        "        feature = self.conv(img)\n",
        "        output = self.fc(feature.view(img.shape[0], -1))\n",
        "        return output "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "t2tC1ih5T4Go",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 454
        },
        "outputId": "3148c776-e2a2-4c48-8d86-ae05f723189e"
      },
      "source": [
        "net = AlexNet()\n",
        "net "
      ],
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "AlexNet(\n",
              "  (conv): Sequential(\n",
              "    (0): Conv2d(1, 96, kernel_size=(11, 11), stride=(4, 4))\n",
              "    (1): ReLU()\n",
              "    (2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
              "    (3): Conv2d(96, 256, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))\n",
              "    (4): ReLU()\n",
              "    (5): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
              "    (6): Conv2d(256, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
              "    (7): ReLU()\n",
              "    (8): Conv2d(384, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
              "    (9): ReLU()\n",
              "    (10): Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
              "    (11): ReLU()\n",
              "    (12): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
              "  )\n",
              "  (fc): Sequential(\n",
              "    (0): Linear(in_features=6400, out_features=4096, bias=True)\n",
              "    (1): ReLU()\n",
              "    (2): Dropout(p=0.5, inplace=False)\n",
              "    (3): Linear(in_features=4096, out_features=4096, bias=True)\n",
              "    (4): ReLU()\n",
              "    (5): Dropout(p=0.5, inplace=False)\n",
              "    (6): Linear(in_features=4096, out_features=10, bias=True)\n",
              "  )\n",
              ")"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 10
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "vpJ5aB7eXD0f",
        "colab_type": "text"
      },
      "source": [
        "### 5.6.3 读取数据"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0JrNxh_rXUI8",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "def load_data_fashion_mnist(batch_size, resize=None, root='~/Datasets/FashionMNIST'):\n",
        "    trans = []\n",
        "    if resize:\n",
        "        trans.append(torchvision.transforms.Resize(size=resize))\n",
        "    trans.append(torchvision.transforms.ToTensor())\n",
        "    transform = torchvision.transforms.Compose(trans)\n",
        "    mnist_train = torchvision.datasets.FashionMNIST(root=root, train=True, download=True, transform=transform)\n",
        "    mnist_test = torchvision.datasets.FashionMNIST(root=root, train=False, download=True, transform=transform)\n",
        "    train_iter = torch.utils.data.DataLoader(mnist_train, batch_size=batch_size, shuffle=True, num_workers=4)\n",
        "    test_iter = torch.utils.data.DataLoader(mnist_test, batch_size=batch_size, shuffle=False, num_workers=4)\n",
        "    return train_iter, test_iter \n",
        "\n",
        "batch_size = 128 \n",
        "train_iter, test_iter = load_data_fashion_mnist(batch_size, resize=224)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "tk9XlsYSZbhT",
        "colab_type": "text"
      },
      "source": [
        "### 5.6.4 训练"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "x5DbWrxcZtUZ",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 118
        },
        "outputId": "7a1a8142-6895-4c72-e186-82494b845a90"
      },
      "source": [
        "lr, num_epochs = 0.001, 5 \n",
        "optimizer = torch.optim.Adam(net.parameters(), lr=lr)\n",
        "d2l.train_ch5(net, train_iter, test_iter, batch_size, optimizer, device, num_epochs)"
      ],
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "training on  cuda\n",
            "epoch 1, loss 0.6709, train acc 0.741, test acc 0.848, time 114.5 sec\n",
            "epoch 2, loss 0.3620, train acc 0.864, test acc 0.874, time 114.1 sec\n",
            "epoch 3, loss 0.3109, train acc 0.885, test acc 0.877, time 114.2 sec\n",
            "epoch 4, loss 0.2844, train acc 0.894, test acc 0.891, time 114.2 sec\n",
            "epoch 5, loss 0.2609, train acc 0.904, test acc 0.899, time 114.1 sec\n"
          ],
          "name": "stdout"
        }
      ]
    }
  ]
}